<?php

namespace app\api\home;

use app\api\model\ImGroup;
use app\api\model\ImGroupUser;
use app\api\validate\GroupUserValidate;
use app\api\validate\GroupValidate;
use app\common\controller\Common;
use app\service\im\Group as ServiceGroup;

class GroupController extends Base
{
    /**
     * Notes:创建群
     * User: tangfen
     * Date: 2023/12/21
     * Time: 19:35
     */
    public function addGroup()
    {
        $params = $this->request->post();
        // 验证
        $validate = new GroupValidate;
        if (!$validate->scene('addGroup')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        if (!empty($params['members'])) {
            $membersArray = explode(',', $params['members']);
            $member = [];
            //初始群成员列表，最多100个
            if (!empty($membersArray)) {
                foreach ($membersArray as $k => $v) {
                    $member['Member_Account'] = $v;
                    $member['Role'] = 'Admin';
                    $membersArray[$k] = $member;
                }
            }
        } else {
            $membersArray = [];//直播群用户为空，不能直接加
        }

        $res = new ServiceGroup();
        $data = $res->createGroup(strval($params['uid']), $params['type'], $params['group_name'], $membersArray, $Introduction = '群公告内容群公告内容群公告内容', $params['url'], $Notification = '');

        if ($data['ActionStatus'] == 'OK') {

            $params['group_id'] = $data['GroupId'];
            $result = (new ImGroup)->InsertCreateGroup($params);//加群数据
            if ($params['type'] == 1) {//社交群才直接加入群成员

                $groupUser = (new ImGroupUser)->AddGroupUser($params, $membersArray);//加群成员数据
                if ($groupUser == true) {
                    return_ajax(1, '操作成功', $result);
                } else {
                    return_ajax(0, '群成员入库失败', $data);
                }
            }
            if ($result == true) {
                return_ajax(1, '操作成功', $result);
            }
            return_ajax(0, '群入库失败', $data);
        } else {
            return_ajax(0, '腾讯建群失败', $data);
        }
    }


    /**
     * Notes:修改群基础信息
     * User: tangfen
     * Date: 2023/12/21
     * Time: 19:35
     */
    public function editGroup()
    {
        $params = $this->request->post();
        // 验证
        $validate = new GroupValidate;
        if (!$validate->scene('updateGroup')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $res = new ServiceGroup();
        $data = $res->updateGroup($params['group_id'], $params['group_name'], $Introduction = '群公告内容群公告内容群公告内容', $params['url'], $Notification = '');

        if ($data['ActionStatus'] == 'OK') {

            $result = (new ImGroup)->UpdateGroup($params);
            if ($result) {
                return_ajax(1, '操作成功', $data);
            }
            return_ajax(0, '更新库失败', $data);
        } else {
            return_ajax(0, '腾讯更新失败', $data);
        }
    }

    /**
     * Notes:解散群聊
     * User: tangfen
     * Date: 2023/12/21
     * Time: 19:35
     */
    public function delGroup()
    {
        $params = $this->request->post();
        // 验证
        $validate = new GroupValidate;
        if (!$validate->scene('delGroup')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $res = new ServiceGroup();
        $data = $res->destroyGroupMember($params['group_id']);

        if ($data['ActionStatus'] == 'OK') {

            $result = (new ImGroup)->DelGroup($params);
            if ($result) {
                return_ajax(1, '操作成功', $data);
            }
            return_ajax(0, '更新库失败', $data);
        } else {
            return_ajax(0, '腾讯更新失败', $data);
        }
    }


    /**
     * Notes:获取群列表
     * User: tangfen
     * Date: 2023/12/21
     * Time: 19:35
     */
    public function groupList()
    {
        $params = $this->request->get();
        // 验证
        $validate = new GroupValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $list = (new ImGroup)->GetGroupList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);
    }


    /**
     * Notes:加入群成员 直播群不能直接邀请加入群成员，只能通过接口记录加入记录
     * User: tangfen
     * Date: 2023/12/21
     * Time: 19:35
     */
    public function joinGroup()
    {
        $params = $this->request->post();

        // 验证
        $validate = new GroupUserValidate;
        if (!$validate->scene('addGroupUser')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $membersArray = explode(',', $params['members']);
        $member = [];
        //初始群成员列表，最多100个
        if (!empty($membersArray)) {
            foreach ($membersArray as $k => $v) {
                $member['Member_Account'] = $v;
//                $member['Role'] = 'Admin'; //设置身份也可以
                $membersArray[$k] = $member;
            }
        }
        if ($params['type'] == 1) {//普通群才直接邀请进群
            $res = new ServiceGroup();
            $data = $res->addGroupMember($params['group_id'], $membersArray);
        }

        $result = (new ImGroupUser)->AddGroupUser($params, $membersArray);
        if ($result) {
            if ($params['type'] == 1) {//普通群才直接邀请进群
                return_ajax(1, '操作成功', $data);
            } else {
                return_ajax(1, '操作成功', $result);
            }
        } else {
            return_ajax(0, '腾讯更新失败', $data);
        }
    }

    /**
     * Notes:退出群 删除群成员
     * User: tangfen
     * Date: 2023/12/21
     * Time: 19:35
     */
    public function exitGroup()
    {
        $params = $this->request->post();

        // 验证
        $validate = new  GroupUserValidate;
        if (!$validate->scene('exitGroup')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $res = new ServiceGroup();
        $data = $res->delGroupMember($params['group_id'], $params['members']);

        if ($data['ActionStatus'] == 'OK') {
            $result = (new ImGroupUser)->ExitGroup($params);
            if ($result) {
                return_ajax(1, '操作成功', $data);
            }
            return_ajax(0, '更新库失败', $data);
        } else {
            return_ajax(0, '腾讯更新失败', $data);
        }
    }


}